package cn.fl.tag.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import cn.lionbridge.center.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
/**
 * 首页欢迎信息
 * @author FUJINGRUN
 */
public class SysIndexWelcomeTag extends TagSupport {

	private static final long serialVersionUID = 3307251307343261121L;

	private static final Logger logger = LoggerFactory.getLogger(SysIndexWelcomeTag.class);
	
	/**
	 * JspWriter
	 */
	private JspWriter out;
	
	/**
	 * 请求
	 */
	//private HttpServletRequest request;
	
	public void init(){
		//request = (HttpServletRequest) pageContext.getRequest();
		out = pageContext.getOut();	
	}
	
	@Override
	public int doStartTag() throws JspException {
		this.init();
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		 try{

			 UserDTO sysUsrDTO = (UserDTO)CurrentThreadContext.getValue(CurrentThreadContext.CURRENT_USER_INFO);
			 String currentRoleNm = "未知";//当前角色
			 String currentUserNm = "未知";//当前用户名称
			 String currentOrgNm = "未知";//当前组织架构
			 //当前所属事业部
			 List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = new ArrayList<>();
			 if(null != sysUsrDTO){
				 String currentRoleNmTmp = (String)CurrentThreadContext.getValue(CurrentThreadContext.CURRENT_ROLE_NM);
				 if(StringUtils.isNotEmpty(currentRoleNmTmp)){
					 currentRoleNm = currentRoleNmTmp;
				 }
				 String currentUserNmTmp = (String)CurrentThreadContext.getValue(CurrentThreadContext.CURRENT_USER_NAME);
				 if(StringUtils.isNotEmpty(currentUserNmTmp)){
					 currentUserNm = currentUserNmTmp;
				 }
				 String currentOrgNmTmp = (String)CurrentThreadContext.getValue(CurrentThreadContext.CURRENT_ORG_DISP_NM);
				 if(StringUtils.isNotEmpty(currentOrgNmTmp)){
					 currentOrgNm = currentOrgNmTmp;
				 }
				 
				 if(CollectionUtils.isNotEmpty(sysUsrDTO.getSysUsrFunAuthRDTOList())){
					 sysUsrFunAuthRDTOList = sysUsrDTO.getSysUsrFunAuthRDTOList();
				 }
			 }
			 
			 
			 StringBuffer buffer = new StringBuffer();
			 buffer.append("<div class=\"l-topmenu-welcome\">");
			 	buffer.append("<ul>");
			 		buffer.append("<li class=\"userAvatarBox\">");
			 			buffer.append("<a href=\"javascript:;\" class=\"userAvatar\"><img src=\"../demos/images/defaultUserAvatar.jpg\"></a>");
			 			buffer.append("<div  class=\"dropdown\">");
				 			buffer.append("<ul>");
			 buffer.append("<li id=\"span_index_welcome_modModify_btn_id\"><a href=\"javascript:;\"><i class=\"topNavico_spirit topNavico_2\"></i>手机号设置</a></li>");
			 buffer.append("<li id=\"span_index_welcome_pwdModify_btn_id\"><a href=\"javascript:;\"><i class=\"topNavico_spirit topNavico_1\"></i>密码设置</a></li>");
				 			/*buffer.append("<li><a href=\"javascript:;\"><i class=\"topNavico_spirit topNavico_2\"></i>权限设置</a></li>");
				 			buffer.append("<li><a href=\"javascript:;\"><i class=\"topNavico_spirit topNavico_3\"></i>个人信息</a></li>");
				 			buffer.append("<li><a href=\"javascript:;\"><i class=\"topNavico_spirit topNavico_4\"></i>皮肤</a></li>");*/
							buffer.append("<li id=\"span_index_welcome_sendFlowMsg_btn_id\"><a href=\"javascript:;\"><i class=\"topNavico_spirit topNavico_3\"></i>短信设置</a></li>");
			 buffer.append("<li id=\"span_index_welcome_loginout_btn_id\"><a href=\"javascript:;\"><i class=\"topNavico_spirit sqBtnIco_7\"></i>用户删除</a></li>");
			 buffer.append("<li id=\"span_index_welcome_exit_btn_id\"><a href=\"javascript:;\"><i class=\"topNavico_spirit topNavico_5\"></i>用户退出</a></li>");
				 			buffer.append("</ul>");
			 			buffer.append("</div>");
			 		buffer.append("</li>");
			 		buffer.append("<li>");
		 			buffer.append("<span>"+currentOrgNm+"</span>");
	                buffer.append("</li>");
	                buffer.append("<li class=\"bar\">|</li>");
			 		buffer.append("<li>");
			 			buffer.append("<span>"+currentRoleNm+"</span>");
                    buffer.append("</li>");
                    buffer.append("<li class=\"bar\">|</li>");
                    buffer.append("<li>");
                    buffer.append("<a href=\"javascript:;\">"+currentUserNm+"</a>");
                     	buffer.append("<span>，您好！</span>");
                    buffer.append("</li>");
                    buffer.append("<li class=\"more\">");
                    buffer.append("<span class=\"moreBtn\"></span>");
                    buffer.append("<div  class=\"dropdown\" style=\"max-height:70vh; overflow-y:auto;\">");
                    	buffer.append("<ul>");
                    		buffer.append("<li>");
                    			buffer.append("<a href=\"javascript:;\">");
	                    			//buffer.append("<span class=\"userAvatar\"><img src=\"../demos/images/defaultUserAvatar.jpg\"></span>");
	                    			buffer.append("<span class=\"userName\">"+currentUserNm+"</span>");
                    			buffer.append("</a>");
                    		buffer.append("</li>");
                    		if(CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOList)){
                    			for (SysUsrFunAuthRDTO sysUsrFunAuthRDTO : sysUsrFunAuthRDTOList) {
                    				if(Integer.valueOf(BaseConstant.IS_YESNO_NO).equals(sysUsrFunAuthRDTO.getRoleIsVis())){
                    					//是不显示的,继续
                    					continue;
                    				}
                    				buffer.append("<li>");
		                    		buffer.append("<span>"+sysUsrFunAuthRDTO.getOrgDispNm()+"</span>");
		                    		buffer.append("<span class=\"bar\">|</span>");
		                    		buffer.append("<span class=\"curPointer\" roleId=\""+sysUsrFunAuthRDTO.getRoleId()+"\" orgId=\""+sysUsrFunAuthRDTO.getOrgId()+"\" id=\"span_index_welcome_role_btn_id\" name=\"span_index_welcome_role_btn_name\">"+sysUsrFunAuthRDTO.getRoleNm()+"</span>");
		                    		buffer.append("</li>");
								}
								buffer.append("<li id=\"span_index_welcome_setDefaultOrgRole_btn_id\" style=\"text-align:right;\"><span class=\"curPointer\">设置默认角色</span></li>");
                    		}
                    		/*buffer.append("<li>");
	                    		buffer.append("<span>狮桥资本</span>");
	                    		buffer.append("<span class=\"bar\">|</span>");
	                    		buffer.append("<span>重卡事业部</span>");
                    		buffer.append("</li>");
                    		
                    		buffer.append("<li>");
                    			buffer.append("<span>北京分公司</span>");
                    			buffer.append("<span class=\"bar\">|</span>");
                    			buffer.append("<span>信审员</span>");
                    		buffer.append("</li>");*/
                    		
                    	buffer.append("</ul>");
                    buffer.append("</div>");
                    buffer.append("</li>");
                buffer.append("</ul>");
            buffer.append("</div>");
		    out.write(buffer.toString());
         } catch (IOException ex){
              throw new JspTagException("Error!");
         }
		 return EVAL_PAGE;
	}
}
